//
//  MyScene.m
//  QBLottery
//
//  Created by Wang Li on 14-1-11.
//  Copyright (c) 2014年 Wang Li. All rights reserved.
//

#import "MyScene.h"
#import "AppDelegate.h"
#import "SKButton.h"

#define MaxScale 2.f
#define MinScale 0.5f

#define FontSize 36

#define StopPer  60

@implementation MyScene
@synthesize lotteryNamesArr=_lotteryNamesArr;
@synthesize spritesArr = _spritesArr;
@synthesize noneTex =_noneTex;
@synthesize selectTex =_selectTex;
-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        

        
        /* Setup your scene here */
        
        _lotteryNamesArr = [NSMutableArray arrayWithArray:@[@"马伟",@"何洁珊",
                                                                    @"郭志强",
                                                                    @"李冯烨",
                                                                    @"左亮亮",
                                                                    @"郑俊克",
                                                                    @"朱成鹏",
                                                                    @"吴洲奇",
                                                                    @"华鸿儒",
                                                                    @"张贤",
                                                                    @"余也",
                                                                    @"王红",
                                                                    @"赵冬",
                                                                    @"周波",
                                                                    @"陈秋明",
                                                                    @"崔岚",
                                                                    @"蔡叶清",
                                                                    @"沙雪",
                                                                    @"杨群",
                                                                    @"张长宏",
                                                                    @"邓马伟",
                                                                    @"张良波",
                                                                    @"刘俊",
                                                                    @"赵凯",
                                                                    @"钟加仁",
                                                                    @"李伟山",
                                                                    @"冷祥武",
                                                                    @"王雪涛",
                                                                    @"黄少意",
                                                                    @"张俊",
                                                                    @"陈敏",
                                                                    @"张涛",
                                                                    @"黄小杰",
                                                                    @"田杉",
                                                                    @"刘念",
                                                                    @"李延",
                                                                    @"张铁",
                                                                    @"张小龙",
                                                                    @"高圣祥",
                                                                    @"雷慈祥",
                                                                    @"胡东升",
                                                                    @"葛雨龙",
                                                                    @"方盛",
                                                                    @"李年丰",
                                                                    @"张亚运",
                                                                    @"杨亭",
                                                                    @"尹祥",
                                                                    @"王里",
                                                                    @"戎彬",
                                                                    @"罗明星",
                                                                    @"张黎",
                                                                    @"陕雪义",
                                                                    @"史廷玉",
                                                                    @"王浩",
                                                                    @"徐金刚",
                                                                    @"秦玉江",
                                                                    @"葛皓",
                                                                    @"王琦"]];
        isInit = NO;
        
        _spritesArr = [[NSMutableArray alloc] init];
        
        
        _actionScaleMax= [SKAction scaleBy:MaxScale duration:1];
        _actionScaleMin= [SKAction scaleBy:MinScale duration:1];
        
        
        _noneTex = [SKTexture textureWithImageNamed:@"btbg.png"];
        _selectTex = [SKTexture textureWithImageNamed:@"bgbg2.png"];
        _selectIndex =-1;
        _FPSIndex =1;
        _PerDo = 10;
    }
    return self;
}

-(void) senceDataInit
{
    for (int i=0;i<[_lotteryNamesArr count];i++) {
        NSString* tName = [_lotteryNamesArr objectAtIndex:i];
        
        CSKSpriteNode *sprite = [CSKSpriteNode spriteNodeWithTexture:_noneTex];
        sprite.position = CGPointMake(80+(i%7)*145,self.frame.size.height-50-i/7*70);
        
        SKLabelNode* tLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        tLabel.text = tName;
        sprite.scaleType = Comm_Style;
        tLabel.fontColor =[NSColor whiteColor];
        tLabel.fontSize =FontSize;
        tLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
        [sprite addChild:tLabel];
        
        [_spritesArr addObject:sprite];
        
        [self addChild:sprite];
        
    }
    
    backButton = [[SKButton alloc] initWithImageNamedNormal:@"nav_done.png" selected:@"nav_select.png"];
    [backButton setPosition:CGPointMake(512, 100)];
    [backButton.title setText:@"开始抽奖"];
    [backButton.title setFontName:@"Chalkduster"];
    [backButton.title setFontSize:36.0];
    [backButton setTouchUpInsideTarget:self action:@selector(buttonAction)];
    [self addChild:backButton];
}


-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    if (!isInit) {
        [self senceDataInit];
        isInit =YES;

    }
    if (_selectIndex >= 0 && _PerDo < StopPer) {
        _FPSIndex++;
        if (_FPSIndex%_PerDo == 0) {
            CSKSpriteNode* spriteLast = [_spritesArr objectAtIndex:_selectIndex];
            [spriteLast setTexture:_noneTex];
            _selectIndex= [self getRandomNum:0 endNum:(int)[_spritesArr count]];
            CSKSpriteNode* spriteNext = [_spritesArr objectAtIndex:_selectIndex];
            [spriteNext setTexture:_selectTex];
            
            if(backButton.runStatus == 2){
                if (_PerDo < StopPer) {
                    _PerDo+=2;
                }
            }
        }
    }else if(_PerDo >=  StopPer && backButton.runStatus == 2){
        backButton.runStatus = 3;
        [backButton.title setText:@"继续抽奖"];
        CSKSpriteNode* spriteNext = [_spritesArr objectAtIndex:_selectIndex];
        [self insertChild:spriteNext atIndex:0];
        [self doAnimate:spriteNext];
    }

}


-(void) doAnimate:(CSKSpriteNode*)spriteNode
{
//    SKAction*  tToAction = nil;
//    if (spriteNode.scaleType == Comm_Style) {
////        int reultNum =[self getRandomNum:1 endNum:2];
////        if (reultNum == 1) {
//            spriteNode.scaleType =Small_Style;
//            tToAction =_actionScaleMin;
////        }else{
////            spriteNode.scaleType =Big_Style;
////            tToAction =_actionScaleMax;
////        }
//    }else if(spriteNode.scaleType == Small_Style){
        spriteNode.scaleType = Comm_Style;
        SKAction*  tToAction =_actionScaleMax;
    
    SKAction*  tMoveAction =[SKAction moveTo:CGPointMake(MaxWidth/2, MaxHeight/2) duration:0.5];
//    }else if(spriteNode.scaleType == Big_Style){
//        spriteNode.scaleType = Comm_Style;
//        tToAction =_actionScaleMin;
//    }
    
    [spriteNode runAction:tMoveAction completion:^{
        [spriteNode runAction:tToAction];

        
    }];
}

-(int) getRandomNum:(int)startNum endNum:(int)eNum
{
    if (startNum >= eNum) {
        NSAlert* tAlert = [NSAlert alertWithMessageText:@"error!" defaultButton:@"ok" alternateButton:@"cancel" otherButton:nil informativeTextWithFormat:@"Nothing"];
        AppDelegate* tAppDel = (AppDelegate*)[NSApplication sharedApplication].delegate;
        [tAlert beginSheetModalForWindow:tAppDel.window modalDelegate:nil didEndSelector:nil contextInfo:nil];
        return 0xffffff;
    }
   return (arc4random() % eNum) + startNum;
}


-(void) buttonAction
{
    NSLog(@"touch in");
    if (backButton.runStatus == 0) {
        if (_selectIndex< 0) {
            [self lotteryInit];
        }
    }else if(backButton.runStatus == 1){
        backButton.runStatus = 2;
        [backButton.title setText:@"产生中"];
    }else if(backButton.runStatus == 2){

    }else if(backButton.runStatus == 3){
        [self removeChildrenInArray:@[[_spritesArr objectAtIndex:_selectIndex]]];
        [_spritesArr removeObjectAtIndex:_selectIndex];
        [self lotteryInit];
    }
}


-(void) lotteryInit
{
    backButton.runStatus = 1;
    _PerDo = 10;
    [backButton.title setText:@"产生大奖"];
    _selectIndex =[self getRandomNum:0 endNum:(int)[_spritesArr count]];
    CSKSpriteNode* spriteNext = [_spritesArr objectAtIndex:_selectIndex];
    [spriteNext setTexture:_selectTex];
}

@end
